package com.vip.buycart.bl.impl;

import java.util.HashMap;
import java.util.List;

import com.vip.buycart.bl.OrderDetailBl;
import com.vip.buycart.dao.OrderDetailDAO;
import com.vip.buycart.entity.OrderDetail;
import com.vip.buycart.entity.Product;
import com.vip.framework.generic.GenericBlImpl;
import com.vip.framework.generic.GenericDao;

public class OrderDetailBlImpl extends GenericBlImpl<OrderDetail> implements
		OrderDetailBl {

	/* (non-Javadoc)
	 * @see com.vip.framework.generic.GenericBlImpl#getAll()
	 */
	@Override
	public List<OrderDetail> getAll() {
		List<OrderDetail> ods = super.getAll();
		Product product=null;
		Long id=null;
		for (OrderDetail orderDetail : ods) {
			id= orderDetail.getProductInfo().getId();
			product = productDao.get(id);
			log.info("set.......");
			orderDetail.setProductInfo(product);
		}
		return ods;
	}

	GenericDao<Product> productDao;
	
	public void setProductDao(GenericDao<Product> productDao){
		this.productDao=productDao;
	}
	
	public OrderDetailDAO getOrderDetailDao(){
		return (OrderDetailDAO)dao;
	}
	
	/**
	 * {@inheritDoc}
	 * 这里需要设置Product和FormOrder,因为Hibernate的lazy延迟特性
	 * 构造的OrderDetail没有Product和FormOrder
	 * 
	 */
	public List<OrderDetail> searchBargain(HashMap<String, String> param,
			int page, int rowsPerPage) {
		
		//注意bargains里的Product和FormOrder都是hibernate创建的匿名类,重新赋值.
		List<OrderDetail> bargains = getOrderDetailDao().searchBargain(param, page, rowsPerPage);
		Product productInfo =null;
		for (OrderDetail orderDetail : bargains) {
			productInfo = orderDetail.getProductInfo();
			Product product = productDao.get(productInfo.getId());
			//这里这样设置是为了阻止使用Hibernate的匿名类,只要不使用不设置为null也可以.
			product.setProductCategory(null);
			orderDetail.setFormOrder(null);
			
			orderDetail.setProductInfo(product);
		}
		
		return bargains;
	}

	
}
